﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace CURE.Handler
{
    /// <summary>
    /// ExpDownHandler 的摘要说明
    /// </summary>
    public class ExpDownHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string path = context.Request.QueryString["path"];
            Down(context, path);
        }

        private bool Down(HttpContext context,string path)
        {
            path = context.Server.MapPath("~"+path);
            int index = path.LastIndexOf("\\");
            string filename = path.Substring(index + 1, path.Length - index - 1);
            FileStream fs = new FileStream(path, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            context.Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开 
            context.Response.AddHeader("Content-Disposition", "attachment; filename="+ filename);
            context.Response.BinaryWrite(bytes);
            context.Response.Flush();
            context.Response.End();
            return true;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}